package com.luo.partner.service.impl;

import com.luo.common.annotation.BaseService;
import com.luo.common.base.BaseServiceImpl;
import com.luo.common.utils.Query;
import com.luo.partner.dao.mapper.TabManagerCommissionMapper;
import com.luo.partner.dao.mapper.ext.ExtTabManagerCommissionMapper;
import com.luo.partner.dao.model.TabManagerCommission;
import com.luo.partner.dao.model.TabManagerCommissionExample;
import com.luo.partner.dao.model.TabMonthlyKnotTemp;
import com.luo.partner.service.TabManagerCommissionService;
import com.luo.system.dao.UserDao;
import com.luo.system.domain.UserDO;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* TabManagerCommissionService实现
* Created by laohu on 2018/9/6.
*/
@Service
@Transactional
@BaseService
public class TabManagerCommissionServiceImpl extends BaseServiceImpl<TabManagerCommissionMapper, TabManagerCommission, TabManagerCommissionExample> implements TabManagerCommissionService {

    private static final Logger LOGGER = LoggerFactory.getLogger(TabManagerCommissionServiceImpl.class);

    @Autowired
    TabManagerCommissionMapper tabManagerCommissionMapper;
    @Autowired
    UserDao userDao;
    @Autowired
    ExtTabManagerCommissionMapper extTabManagerCommissionMapper;

	@Override
	public TabManagerCommission get(Long id) {
		// TODO Auto-generated method stub
		return tabManagerCommissionMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<TabManagerCommission> list() {
		// TODO Auto-generated method stub
		return tabManagerCommissionMapper.selectByExample(new TabManagerCommissionExample());
	}

	@Override
	public int save(TabManagerCommission role) {
		// TODO Auto-generated method stub
		return tabManagerCommissionMapper.insertSelective(role);
	}

	@Override
	public int update(TabManagerCommission role) {
		// TODO Auto-generated method stub
		return tabManagerCommissionMapper.updateByPrimaryKeySelective(role);
	}

	@Override
	public int remove(Long id) {
		// TODO Auto-generated method stub
		return tabManagerCommissionMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<TabManagerCommission> list(Long userId) {
		TabManagerCommissionExample example = new TabManagerCommissionExample();
		example.or().andUserIdEqualTo(userId);
		return tabManagerCommissionMapper.selectByExample(example);
	}

	@Override
	public int batchRemove(Long[] ids) {
		try {
			for (Long id : ids) {
				tabManagerCommissionMapper.deleteByPrimaryKey(id);
			}
		} catch (Exception e) {
			return 0;
		}
		return 1;
	}

	@Override
	public int count(Query query) {
		return 0;
	}


	@Override
	public int countList(Map<String, Object> map) {
		return extTabManagerCommissionMapper.countList(map);
	}
	
	@Override
	public List<TabManagerCommission> findList(Map<String, Object> map) {
		return extTabManagerCommissionMapper.findList(map);
	}

	@Override
	@Transactional
	public void applyCommission(Long id, Integer checkStatus) throws RuntimeException {
		TabManagerCommission record = tabManagerCommissionMapper.selectByPrimaryKey(id);
		record.setCheckStatus(checkStatus);
		record.setCheckTime(new Date());
		if (tabManagerCommissionMapper.updateByPrimaryKey(record) <= 0) {
			throw new RuntimeException("修改审核记录状态失败");
		}
		Map<String,Object> paramMap = new HashMap<String, Object>();
		paramMap.put("user_id", record.getUserId());
		paramMap.put("commission", record.getCommission());
		if (checkStatus == 1) {
			// 增加用户的withdraw_commission，减少freeze_commission
			if (userDao.addWithdrawCommission(paramMap) <= 0) {
				throw new RuntimeException("修改合伙人信息失败");
			}
		}else if (checkStatus == 2) {
			// 增加用户的un_withdraw_commission，减少freeze_commission
			if (userDao.addUnWithdrawCommission(paramMap) <= 0) {
				throw new RuntimeException("修改合伙人信息失败");
			}
		}else{
			throw new RuntimeException("选择的状态有误");
		}
	}

}